package com.hsgene.dict.service.impl;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.dict.condition.SysDictCondition;
import com.hsgene.dict.condition.SysDictQueryCondition;
import com.hsgene.dict.domain.SysDict;
import com.hsgene.dict.dto.SysDictDto;
import com.hsgene.dict.dto.SysDictSaveDto;
import com.hsgene.dict.persistence.SysDictPersistence;
import com.hsgene.dict.service.DictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc:
 * @author: fuxiang
 * @date: 2018/9/25 15:32
 */
@Service
public class DictServiceImpl implements DictService {

    @Resource
    private SysDictPersistence sysDictPersistence;

    /**
     * @param id 字典逻辑主键id
     * @return com.hsgene.dict.dto.SysDictDto
     * @description 根据id查询字典信息
     * @author maodi
     * @createDate 2018/11/29 9:24
     */
    @Override
    public SysDictDto select(String id) {
        return sysDictPersistence.select(id);
    }

    /**
     * @param id 字典逻辑主键id
     * @return java.lang.Integer
     * @description 根据id查询字典数量
     * @author maodi
     * @createDate 2018/11/29 15:06
     */
    @Override
    public Integer queryCountById(String id) {
        return sysDictPersistence.queryCountById(id);
    }

    /**
     * @param id 字典逻辑主键id
     * @return void
     * @description 根据字典id删除字典
     * @author maodi
     * @createDate 2018/11/29 9:24
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void delete(String id) {
        sysDictPersistence.delete(id, new Date());
    }

    /**
     * @param ids id数组
     * @return void
     * @description 根据字典id数组批量删除字典
     * @author maodi
     * @createDate 2018/11/29 15:00
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void deleteMulti(String[] ids) {
        sysDictPersistence.deleteMulti(ids, new Date());
    }

    /**
     * @param sysDict 字典信息
     * @return void
     * @description 新增字典信息
     * @author maodi
     * @createDate 2018/11/29 9:25
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void insert(SysDict sysDict) {
        String label = sysDict.getValue();
        String dictNameId = sysDict.getLabel();
        String typeId = sysDict.getType();
        String type = sysDictPersistence.queryClassfyNameByClassfyId(typeId);
        String id = sysDictPersistence.queryBylabelAndDictNameIdAndType(label, dictNameId, type);
        String userId = sysDict.getCreateBy();
        Date date = new Date();
        if (StringUtils.isBlank(id)) {
            SysDictSaveDto dto = new SysDictSaveDto();
            dto.setId(sysDict.getId());
            dto.setType(type);
            dto.setLabel(sysDict.getValue());
            dto.setDictNameId(sysDict.getLabel());
            dto.setValue(String.valueOf(System.currentTimeMillis()));
            dto.setParentId(sysDict.getParentId());
            dto.setCreateBy(userId);
            dto.setCreateDate(date);
            dto.setUpdateBy(userId);
            dto.setUpdateDate(date);
            sysDictPersistence.insert(dto);
        } else {
            sysDictPersistence.recoveryData(id, userId, date, userId, date);
        }
    }

    /**
     * @param sysDict 字典信息
     * @return void
     * @description 修改字典信息
     * @author maodi
     * @createDate 2018/11/29 9:26
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void update(SysDict sysDict) {
        String typeId = sysDict.getType();
        String type = sysDictPersistence.queryClassfyNameByClassfyId(typeId);
        String userId = sysDict.getCreateBy();
        Date date = new Date();
        SysDictSaveDto dto = new SysDictSaveDto();
        dto.setId(sysDict.getId());
        dto.setType(type);
        dto.setLabel(sysDict.getValue());
        dto.setDictNameId(sysDict.getLabel());
        dto.setParentId(sysDict.getParentId());
        dto.setCreateBy(userId);
        dto.setCreateDate(date);
        dto.setUpdateBy(userId);
        dto.setUpdateDate(date);
        sysDictPersistence.update(dto);
    }

    /**
     * @param condition 查询条件
     * @return java.util.List<com.hsgene.dict.dto.SysDictDto>
     * @description 字典列表/条件查询字典列表
     * @author maodi
     * @createDate 2018/11/29 10:25
     */
    @Override
    public List<SysDictDto> queryDicts(SysDictCondition condition) {
        SysDictQueryCondition queryCondition = new SysDictQueryCondition();
        String typeId = condition.getTypeId();
        String type = sysDictPersistence.queryClassfyNameByClassfyId(typeId);
        queryCondition.setType(type);
        queryCondition.setDictNameId(condition.getLabelId());
        queryCondition.setLabel(condition.getValue());
        queryCondition.setLimitHelper(condition.getLimitHelper());
        queryCondition.setOrderHelper(condition.getOrderHelper());
        return sysDictPersistence.queryDicts(queryCondition);
    }

    /**
     * @param condition 查询条件
     * @return java.lang.Integer
     * @description 查询字典数量/条件查询字典数量
     * @author maodi
     * @createDate 2018/11/29 9:27
     */
    @Override
    public Integer queryDictCount(SysDictCondition condition) {
        SysDictQueryCondition queryCondition = new SysDictQueryCondition();
        String typeId = condition.getTypeId();
        String type = sysDictPersistence.queryClassfyNameByClassfyId(typeId);
        queryCondition.setType(type);
        queryCondition.setDictNameId(condition.getLabelId());
        queryCondition.setLabel(condition.getValue());
        return sysDictPersistence.queryDictCount(queryCondition);
    }

    /**
     * @param id         字典逻辑主键id
     * @param type       字典类型
     * @param label      字典值
     * @param dictNameId 字典名称id
     * @return java.lang.Integer
     * @description 根据值查询字典信息数量/排除当前id根据值查询字典信息数量
     * @author maodi
     * @createDate 2018/11/29 14:37
     */
    @Override
    public Integer queryCountByValue(String id, String type, String dictNameId, String label) {
        type = sysDictPersistence.queryClassfyNameByClassfyId(type);
        return sysDictPersistence.queryCountByValue(id, type, dictNameId, label);
    }


    /**
     * @param
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @description 获取字典所有分类
     * @author maodi
     * @createDate 2018/11/28 14:44
     */
    @Override
    public List<Map<String, Object>> queryAllClassifies() {
        return sysDictPersistence.queryAllClassifies();
    }

    /**
     * @param id sys_dict_addtion的id
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @description 根据字典名称id获取上级节点
     * @author maodi
     * @createDate 2018/11/28 14:43
     */
    @Override
    public List<Map<String, Object>> queryValuesByLabel(String id) {
        return sysDictPersistence.queryValuesByLabel(id);
    }

    /**
     * @param id 字典逻辑主键id
     * @return java.lang.Integer
     * @description 查询字典使用数量
     * @author maodi
     * @createDate 2018/11/29 14:07
     */
    @Override
    public Integer queryDictUsedCountById(String id) {
        return sysDictPersistence.queryDictUsedCountById(id);
    }

    /**
     * @param id 字典逻辑主键id
     * @return void
     * @description 增加一次字典使用数量
     * @author maodi
     * @createDate 2018/11/29 14:09
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void addDictUsedCount(String id) {
        sysDictPersistence.addDictUsedCount(id, new Date());
    }

    /**
     * @param id 字典逻辑主键id
     * @return void
     * @description 减少一次字典使用数量
     * @author maodi
     * @createDate 2018/11/29 14:12
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void reduceDictUsedCount(String id) {
        sysDictPersistence.reduceDictUsedCount(id, new Date());
    }

    /**
     * @param ids 字典逻辑主键id数组
     * @return void
     * @description 批量增加一次字典使用数量
     * @author maodi
     * @createDate 2018/11/29 14:09
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void addMultiDictUsedCount(String[] ids) {
        sysDictPersistence.addMultiDictUsedCount(ids, new Date());
    }

    /**
     * @param ids 字典逻辑主键id数组
     * @return void
     * @description 批量减少一次字典使用数量
     * @author maodi
     * @createDate 2018/11/29 14:12
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void reduceMultiDictUsedCount(String[] ids) {
        sysDictPersistence.reduceMultiDictUsedCount(ids, new Date());
    }

    /**
     * @param classifyId 分类id
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @description 根据字典分类id获取字典名称下拉框数据源
     * @author maodi
     * @createDate 2018/12/3 15:13
     */
    @Override
    public List<Map<String, Object>> queryLabelsByClassifyId(String classifyId) {
        return sysDictPersistence.queryLabelsByClassifyId(classifyId);
    }

    /***
     * 根据字典名称查询所有字典
     * @author jcl
     * @date 2018/12/4 16:00
     * @param dictAddtionId 字典名称的id
     * @return
     */
    @Override
    public ApiResult<List<SysDictDto>> valueByDictAddtionId(String dictAddtionId) {
        List<SysDictDto> dictDtos = sysDictPersistence.valueByDictAddtionId(dictAddtionId);
        return new ApiResult<>(200, "success", "获取数据成功", dictDtos);
    }

    /**
     * @param ids id数组
     * @return com.hsgene.common.util.result.ApiResult<java.util.List<com.hsgene.dict.dto.SysDictDto>>
     * @description 根据ids获取字典list
     * @author FX
     * @createDate 2018/12/5 17:08
     */
    @Override
    public ApiResult<List<SysDictDto>> selectIds(String[] ids) {
        return ApiResult.succ(sysDictPersistence.selectByIds(ids));
    }
}
